$(function () {
    tagsCloud();
});
function tagsCloud() {
    $.ajax({
            type: "GET",
            url: "/tag/api/tagCloud",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function (data) {
                console.log(data);
                if (data.status === "200" && data.tagList.length != 0) {
                    var words = [];
                    for (var i = 0; i < data.tagList.length; i++) {
                        var tid = "";
                        var tagObj = {};
                        var text = "text";
                        var weight = "weight";
                        var handlers = "handlers";
                        tagObj[text] = data.tagList[i].tagName;
                        tagObj[weight] = (data.tagList[i].renum / 10) + 5;
                        tid = data.tagList[i].tagId;
                        tagObj[handlers] = {
                            click: function () {
                                $("#keywords").hide();
                                $.ajax({
                                    type: "GET",
                                    url: "/front/tagCount?tagId=" + tid,
                                    dataType: "json",
                                    contentType: "application/json; charset=utf-8",
                                    success: function (data) {
                                        console.log(data);
                                        layui.use('laypage', function () {
                                            var laypage = layui.laypage;
                                            //执行一个laypage实例
                                            laypage.render({
                                                elem: 'loadPage', //注意，这里的 loadPage 是 ID，不用加 # 号
                                                count: data.data, //数据总数，从服务端得到
                                                curr: 1,
                                                limit: 5,//每页条数
                                                jump: function (obj, first) {
                                                    //obj包含了当前分页的所有参数，比如：
                                                    //得到当前页，以便向服务端请求对应页的数据。console.log(obj.curr);
                                                    //得到每页显示的条数 console.log(obj.limit);
                                                    $.ajax({
                                                        type: "GET",
                                                        url: "/front/showTagList?page=" + obj.curr + '&limit=' + obj.limit + '&tagId=' + tid,
                                                        dataType: "json",
                                                        contentType: "application/json; charset=utf-8",
                                                        success: function (data) {
                                                            console.log(data);
                                                            var indexblog = '';
                                                            if (data.code === "200") {
                                                                $("#home").empty();
                                                                for (var i = 0; i < data.data.records.length; i++) {
                                                                    categorys = data.data.records[i].category;
                                                                    if (data.data.records[i].postThumbnail === "") {
                                                                        //纯文字版本
                                                                        indexblog =
                                                                            '<div class="home-article-wrapper clearfix">' +
                                                                            '<div class="home-article" data-cover="false">' +
                                                                            '<div class="home-article-inner" data-cover="false">' +
                                                                            '<a class="passage-meta">' +
                                                                            '<span>' +
                                                                            '<i class="fa fa-calendar"></i>' + fmt(data.data.records[i].postDate) + '' +
                                                                            '</span>' +
                                                                            '| <i class="fa fa-location-arrow"></i><a href="/web/categoryPage?categoryId=' + categorys.cateId + '"> ' + categorys.cateName + '' +
                                                                            '</a></span>' +
                                                                            '<span>' +
                                                                            '| <i class="fa fa-unlock-alt"></i>UNLOCK' +
                                                                            '</span>' +
                                                                            // '</div>' +
                                                                            '<h1 class="home-article-title">' +
                                                                            '<a href="single.html?postId=' + data.data.records[i].postId + '&page=1&limit=5">' + data.data.records[i].postTitle + '</a>' +
                                                                            '</h1>' +
                                                                            '<div class="home-article-content passage-article">' +
                                                                            '' + data.data.records[i].postContent.substring(0, 200) + '' +
                                                                            '</div>' +
                                                                            '<a class="home-article-read" href="single.html?postId=' + data.data.records[i].postId + '&page=1&limit=5">' +
                                                                            'README MORE >>>' +
                                                                            '</a>' +
                                                                            // '<div class="passage-tags">' +
                                                                            // '<a href="/tags/第三方库/"><i class="fa fa-tags"></i>第三方库</a>' +
                                                                            // '</div>' +
                                                                            '</div>' +
                                                                            '</div>';
                                                                        $("#home").append(indexblog);
                                                                    }
                                                                    if (data.data.records[i].postThumbnail !== "") {
                                                                        //有图版本
                                                                        //var articleImg = data.data.records[i].postThumbnail !== "" ? data.data.records[i].postThumbnail : '/static/images/posts/6.jpg';
                                                                        indexblog =
                                                                            '<div class="home-article-wrapper clearfix">' +
                                                                            '<div class="home-article" data-cover="true">' +
                                                                            '<div class="home-article-inner" data-cover="true">' +
                                                                            '<div class="passage-meta">' +
                                                                            '<span>' +
                                                                            '<i class="fa fa-calendar"></i>' + fmt(data.data.records[i].postDate) + '' +
                                                                            '</span>' +
                                                                            '| <i class="fa fa-location-arrow"></i><a href="/web/categoryPage?categoryId=' + categorys.cateId + '"> ' + categorys.cateName + '' +
                                                                            '</a></span>' +
                                                                            '<span>' +
                                                                            '| <i class="fa fa-unlock-alt"></i>UNLOCK' +
                                                                            '</span>' +
                                                                            '</div>' +
                                                                            '<h1 class="home-article-title">' +
                                                                            '<a href="single.html?postId=' + data.data.records[i].postId + '&page=1&limit=5">' + data.data.records[i].postTitle + '</a>' +
                                                                            '</h1>' +
                                                                            '<div class="home-article-content passage-article">' +
                                                                            '' + data.data.records[i].postContent.substring(0, 100) + '' +
                                                                            '</div>' +
                                                                            '<a class="home-article-read" href="single.html?postId=' + data.data.records[i].postId + '&page=1&limit=5">' +
                                                                            '    README MORE >>>' +
                                                                            '</a>' +
                                                                            // '<div class="passage-tags">' +
                                                                            // '<a href="/tags/第三方库/"><i class="fa fa-tags"></i>第三方库</a>' +
                                                                            // '</div>' +
                                                                            '<div class="home-article-cover">' +
                                                                            '<img class="textimg" src="' + data.data.records[i].postThumbnail + '">' +
                                                                            '</div>' +
                                                                            '</div>' +
                                                                            '</div>';
                                                                        $("#home").append(indexblog);
                                                                    }

                                                                }
                                                            } else {
                                                                //layer.msg(data.msg, {time: 1 * 1000});
                                                            }
                                                        }
                                                    });
                                                    //首次不执行
                                                    if (!first) {
                                                        //do something
                                                    }
                                                }
                                            });
                                        });
                                    }
                                });
                            }
                        }
                        ;
                        words.push(tagObj);
                    }
                    $("#keywords").jQCloud(words);
                } else {
                    //layer.msg(data.msg, {time: 1 * 1000});
                }
            }
        }
    )
};

//时间戳
function fmt(timestamp) {
    //13位时间戳转换
    var date = new Date(timestamp / 1);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    var h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    var minute = date.getMinutes();
    var second = date.getSeconds();
    minute = minute < 10 ? ('0' + minute) : minute;
    second = second < 10 ? ('0' + second) : second;
    return y + '-' + m + '-' + d + " " + h + ":" + minute + ":" + second;
}